package com.sanxiau.dormitory.yang.controller;

import com.sanxiau.dormitory.yang.entity.Student;
import com.sanxiau.dormitory.yang.face.Base64Util;
import com.sanxiau.dormitory.yang.service.StudentMessageService;
import com.sanxiau.dormitory.yang.vo.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import javax.servlet.http.HttpSession;
import javax.xml.transform.Result;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/studentMessage")
public class StudentMessageController {

    @Autowired
    StudentMessageService studentMessageService;

    @RequestMapping("/showMessage")
    public String  showMessage(Model model,HttpSession session){
        Student student = studentMessageService.showMessage(session);
        model.addAttribute("student",student);
        String photo = "";
        if (null != student.getPhoto()) {
            photo = Base64Util.encode(student.getPhoto());
        }
        model.addAttribute("photo", photo);
        return "yang/studentMessage";
    }

    @RequestMapping("/updateStudent")
    public String  updateStudent(Student student,Model model,HttpSession session){
        ResultInfo resultInfo = studentMessageService.updateStudent(student);
        model.addAttribute("message",resultInfo.getMessage());
        Student student1 = studentMessageService.showMessage(session);
        model.addAttribute("student",student1);
        System.out.println(student1);
        String photo = "";
        if (null != student1.getPhoto()) {
            photo = Base64Util.encode(student1.getPhoto());
        }
        model.addAttribute("photo", photo);
        return "yang/studentMessage";
    }
    @ResponseBody
    @RequestMapping("/addPhoto")
    public Map<String,Object> addPhoto(@RequestParam(value = "projectImg",required = true) MultipartFile file,Model model,HttpSession session){

        Map map = new HashMap<String,Object>();
        byte[] photo = new byte[0];
        try {
             photo = file.getBytes();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("photo:"+photo);
        int num = studentMessageService.addPhoto(photo,session.getAttribute("userName")+"");
        if (num>0){
            map.put("msg","error");
            map.put("code",0);
            model.addAttribute("message","图片添加成功");
        }else {
            map.put("msg","ok");
            map.put("code",200);
            model.addAttribute("message","图片添加失败");
        }
        return map;
    }

}
